﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class List : System.Web.UI.Page
{

    static int MAXSIZE = 50;
    string temp, temp2;
    int Rows, Columns, i;
    int[] AuctionIDNum = new int[MAXSIZE];
    string[] Seller = new string[MAXSIZE];
    string[] AuctionTitle = new string[MAXSIZE];
    int[] ItemIDNum = new int[MAXSIZE];
    DateTime[] Endtime = new DateTime[MAXSIZE];
    string[] CurrentWinner = new string[MAXSIZE];
    string[] Bidprice = new string[MAXSIZE];
    string[] Picture = new string[MAXSIZE];

    protected void Page_Load(object sender, EventArgs e)
    {
//        if (!IsPostBack) 
        if (Session["userid"] != null)
        {
            ReadRecords();
        }
    }

    private void ReadRecords()
    {

        OleDbConnection conn = null;
        OleDbDataReader reader = null;
        try
        {
            conn = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=" + "|DataDirectory|BayouFarmersDB.accdb");
//            conn = new OleDbConnection(ConfigurationManager.AppSettings["ItemsDataSet"]);
            conn.Open();

            OleDbCommand cmd = new OleDbCommand("Select * FROM Auctions WHERE closed='no' ORDER BY ends", conn);
            reader = cmd.ExecuteReader();

            //Count the number of items
            int index = 0;
            while (reader.Read())
            {
                AuctionIDNum[index] = (int)reader[0];
                index++;
            }
            reader.Close();

            Rows = index;
            Columns = 7;

            //Fill data arrays
            i = 0;
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                    AuctionIDNum[i] = (int)reader["ID"];
                    Seller[i] = (string)reader["seller"];
                    AuctionTitle[i] = (string)reader["title"];
                    ItemIDNum[i] = (int)reader["item"];
                    Endtime[i] = (DateTime)reader["ends"];
                    CurrentWinner[i] = (string)reader["currentWinner"];
                    Bidprice[i] = String.Format("{0:0.00}", reader["currentBid"]);
                    i++;
            }
            reader.Close();

//Get webpage address of images.  Must also parse the "description" category to remove the # symbols
//from the Access database.
            for (i = 0; i < Rows; i++)
            {
                temp = "Select * FROM Items WHERE itemNumber=" + ItemIDNum[i].ToString();
                cmd = new OleDbCommand(temp, conn);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    temp2 = (string)reader["description"];
                    string[] temp3 = temp2.Split('#');
//                    Picture[i] = "<img src=\"" + temp3[1] + "\" height=\"100\" />";
                    Picture[i] = "<img src=\"" + temp3[1] + "\"/>";
                }
               
            }


            if (Rows > 0)
            {
                CreateDynamicTable(Rows, Columns);
            }

        }

        finally
        {
            if (reader != null) reader.Close();
            if (conn != null) conn.Close();
        }
    }

    private void CreateDynamicTable(int tblRows, int tblCols)
    {

        ListTable.Controls.Clear();
        // Create a Table and set its properties 
        Table tbl = new Table();
        // Add the table to the placeholder control
        ListTable.Controls.Add(tbl);
        TableRow headerRow = new TableRow();

        for (int j = 0; j < tblCols; j++)
        {
            TableHeaderCell header = new TableHeaderCell();
            if (j == 0)
            {
                header.Text = "Auction ID";
                header.Width = 80;
            }
            if (j == 1)
            {
                header.Text = "Seller ID";
                header.Width = 80;
            }
            if (j == 2)
            {
                header.Text = "Auction Title";
                header.Width = 120;
            }
            if (j == 3)
            {
                header.Text = "End Date/Time";
                header.Width = 160;
            }
            if (j == 4)
            {
                header.Text = "Current Winner";
                header.Width = 120;
            }
            if (j == 5)
            {
                header.Text = "Current Bid Price";
                header.Width = 120;
            }
            if (j == 6)
            {
                header.Text = "Picture";
                header.Width = 100;
            }

            header.BorderStyle = BorderStyle.None;
            headerRow.Cells.Add(header);
        }
        tbl.Rows.AddAt(0, headerRow);

        // Now iterate through the table and add your controls 

        for (int i = 0; i < tblRows + 1; i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < tblCols; j++)
            {
                if (i == tblRows)
                {
                    TableCell tc = new TableCell();
                    Label title = new Label();

                    if (j == 0)
                    { title.Text = ""; }
                    if (j == 1)
                    { title.Text = ""; }
                    if (j == 2)
                    { title.Text = ""; }
                    if (j == 3)
                    { title.Text = ""; }
                    if (j == 4)
                    { title.Text = ""; }
                    if (j == 5)
                    { title.Text = ""; }
                    if (j == 6)
                    { title.Text = ""; }
                    tc.Controls.Add(title);
                    tr.Cells.Add(tc);
                }
                else
                {
                    TableCell tc = new TableCell();
                    Label title = new Label();

                    if (j == 0)
                    { title.Text = "<a href=\"SpecificAuction.aspx?AuctionID=" + AuctionIDNum[i] + "\">" + AuctionIDNum[i].ToString() + "</a>";}
                    if (j == 1)
                    { title.Text = Seller[i]; }
                    if (j == 2)
                    { title.Text = AuctionTitle[i]; }
                    if (j == 3)
                    { title.Text = Endtime[i].ToString(); }
                    if (j == 4)
                    { title.Text = CurrentWinner[i]; }
                    if (j == 5)
                    { title.Text = Bidprice[i]; }
                    if (j == 6)
                    { 
                        title.Text = Picture[i]; 
//                        tc.Height = 100; 
                    }
                    title.BorderStyle = BorderStyle.None;
                    // Add the control to the TableCell
                    tc.Controls.Add(title);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }
            }
            // Add the TableRow to the Table
            tbl.Rows.Add(tr);
        }

        // This parameter helps determine in the LoadViewState event,
        // whether to recreate the dynamic controls or not

        ViewState["dynamictable"] = true;
    }

    protected void exitBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }

}